{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#导入numpy\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n [5 6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  4  6  8]\n [10 12 14 16]]\n==========\n[[0.5 1.  1.5 2. ]\n [2.5 3.  3.5 4. ]]\n==========\n[[ 3  4  5  6]\n [ 7  8  9 10]]\n<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "#s不会修改原来得数组\n",
    "print(arr * 2)\n",
    "print(\"=\" * 10)\n",
    "print(arr / 2)\n",
    "print(\"=\" * 10)\n",
    "print(arr + 2)\n",
    "print(type(arr))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n [5 6 7 8]]\n==========\n[1 2 3 4]\n==========\n[[1 2]\n [5 6]]\n==========\n[[7 8]]\n==========\n[[2 4]\n [6 8]]\n==========\n[[8 7 6 5]\n [4 3 2 1]]\n科技\n"
     ]
    }
   ],
   "source": [
    "# arr[开始位置:结束位置:步长,开始位置:结束位置:步长]\n",
    "print(arr)\n",
    "print(\"=\" * 10)\n",
    "print(arr[0])\n",
    "print(\"=\" * 10)\n",
    "print(arr[:, 0:2])\n",
    "print(\"=\" * 10)\n",
    "print(arr[1:, 2:])\n",
    "print(\"=\" * 10)\n",
    "print(arr[:, 1::2])\n",
    "print(\"=\" * 10)\n",
    "print(arr[::-1, ::-1])\n",
    "\n",
    "s = \"shujia科技\"\n",
    "print(s[-2:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n [4 5 6]\n [7 8 9]]\n==========\n[[1 2 3]\n [0 0 0]\n [7 8 9]]\n==========\n[[ True  True  True]\n [ True  True  True]\n [False False False]]\n==========\n[1 2 3 0 0 0]\n==========\n[0 0 0]\n[3]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "print(arr)\n",
    "print(\"=\" * 10)\n",
    "arr[1] = 0  #修改数据\n",
    "print(arr)\n",
    "print(\"=\" * 10)\n",
    "print(arr < 5)\n",
    "print(\"=\" * 10)\n",
    "#布尔索引，两个数组元素要一样\n",
    "print(arr[arr < 5])\n",
    "print(\"=\" * 10)\n",
    "print(arr[arr == 0])\n",
    "print(\"=\" * 10)\n",
    "print(arr[(arr < 5) & (arr > 2)])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 4]\n [5 6 7 8]]\n==========\n[[1 2]\n [3 4]\n [5 6]\n [7 8]]\n==========\n[[1 2 3 4]\n [5 6 7 8]]\n==========\n[[1 5]\n [2 6]\n [3 7]\n [4 8]]\n==========\n[[4 3 2 1]\n [8 7 6 5]]\n[[5 6 7 8]\n [1 2 3 4]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
    "print(arr)\n",
    "print(\"=\" * 10)\n",
    "print(arr.reshape([4, 2]))  #改变矩阵形状\n",
    "print(\"=\" * 10)\n",
    "print(arr)\n",
    "print(\"=\" * 10)\n",
    "print(arr.T)  #转置\n",
    "print(\"=\" * 10)\n",
    "print(np.fliplr(arr))\n",
    "print(np.flipud(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 4 4]\n [6 6 6]]\n[[-2 -2 -2]\n [-2 -2 -2]]\n[[3 3 3]\n [8 8 8]]\n[[0.33333333 0.33333333 0.33333333]\n [0.5        0.5        0.5       ]]\n"
     ]
    }
   ],
   "source": [
    "#对位运算  对应位置计算\n",
    "\n",
    "a = np.array([[1, 1, 1], [2, 2, 2]])\n",
    "b = np.array([[3, 3, 3], [4, 4, 4]])\n",
    "print(a + b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(a // b)  #向下取整\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.         0.69314718 1.09861229 1.38629436 1.60943791]\n[ 0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427]\n"
     ]
    }
   ],
   "source": [
    "#数学运算\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "print(np.log(arr))\n",
    "print(np.sin(arr))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  1  2  1  1  1 -5  3]\n[[1 2 3]\n [5 6 7]\n [8 3 6]]\n[[ 4  4  4]\n [ 3 -3 -1]]\n"
     ]
    }
   ],
   "source": [
    "#diff  差分\n",
    "arr = np.array([1, 2, 3, 5, 6, 7, 8, 3, 6])\n",
    "print(np.diff(arr))\n",
    "print(arr.reshape([3, 3]))\n",
    "print(np.diff(arr.reshape([3, 3]), axis=0))  #axis  1  横向差分，0  纵向差分\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n [5 6 7]\n [8 3 6]]\n==========\n[3 7 8]\n==========\n[1 2 3]\n==========\n[2.         6.         5.66666667]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 5, 6, 7, 8, 3, 6]).reshape([3, 3, ])\n",
    "print(arr)\n",
    "print(\"=\" * 10)\n",
    "print(np.max(arr, axis=1))\n",
    "print(\"=\" * 10)\n",
    "print(np.min(arr, axis=0))\n",
    "print(\"=\" * 10)\n",
    "print(np.average(arr, axis=1))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -2.66312955  -7.16737644  -0.91365399  -1.28626673  -4.75418945\n    1.27467757]\n [ -0.99338383   7.5645059    0.29034127  -9.61812414   5.65677692\n    1.46178661]\n [ -1.65667185   7.88095903  -3.89448564  -1.49663679  -4.4565303\n    5.06366237]\n [-10.89641996 -10.12139679  -9.63042935  -0.95669662  -3.96142236\n   -1.7300321 ]\n [ -5.71776293   4.72436366   3.59520273   2.63080786   0.71260806\n   -3.85365807]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.random.normal(loc=0, scale=5, size=(5, 6))\n",
    "print(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n [3 4 5]]\n[[0 1]\n [2 3]\n [4 5]]\n[[10 13]\n [28 40]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(6).reshape([2, 3])\n",
    "b = np.arange(6).reshape([3, 2])\n",
    "print(a)\n",
    "print(b)\n",
    "print(a.dot(b))  #dot  矩阵相乘\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
